package lance.honeydew.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.JoinType;

import lance.honeydew.bean.CategoryBig;
import lance.honeydew.bean.CategoryMedium;
import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.PaymentMethod;
import lance.honeydew.bean.TransportMethod;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;

public class CategoryBigDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(CategoryBigDAO.class);
	
	/**
	 * @param seqId
	 * @return
	 */
	public CategoryBig findCategoryBigBySeqId(Long seqId) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CategoryBig.class);
		CategoryBig result = null;
		
		try {
			
			criteria.add(Restrictions.eq(CategoryBig.PROP_SEQID, seqId));
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(CategoryBig.PROP_SEQID), "_seqid")
					.add(Projections.property(CategoryBig.PROP_NAME), "_name"));
			criteria.setResultTransformer(Transformers.aliasToBean(CategoryBig.class));
			
			result = (CategoryBig) this.getHibernateTemplate().
					findByCriteria(criteria).get(0);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryCategoryBigByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<CategoryBig> queryCategoryBigByQb(CategoryBig qb, int pageNum, int pageSize) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CategoryBig.class);
		List<CategoryBig> list = null;
		
		try {
			
			if (StringUtils.isNotEmpty(qb.getName())) {
				criteria.add(Restrictions.like(CategoryBig.PROP_NAME, qb.getName(), MatchMode.ANYWHERE));
			}
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(CategoryBig.PROP_SEQID), "_seqid")
					.add(Projections.property(CategoryBig.PROP_NAME), "_name"));
			criteria.setResultTransformer(Transformers.aliasToBean(CategoryBig.class));
			
			list = (List<CategoryBig>) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * findAllCategoryBigList
	 * @return
	 */
	public List<CategoryBig> findAllCategoryBigList() {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CategoryBig.class);
		List<CategoryBig> list = null;
		
		try {
			
			criteria.setProjection(Projections.projectionList()
					.add(Projections.property(CategoryBig.PROP_SEQID), "_seqid")
					.add(Projections.property(CategoryBig.PROP_NAME), "_name"));
			criteria.setResultTransformer(Transformers.aliasToBean(CategoryBig.class));
			
			list = (List<CategoryBig> ) this.getHibernateTemplate().
					findByCriteria(criteria);
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return list;
	}
	
	/**
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<CategoryMedium> queryCategoryMediumByQb(CategoryBig qb, int pageNum, int pageSize) {
		
		List<CategoryMedium> list = null;
		
		try {
			
			String sqlString = " select b from CategoryBig b " +
					" where 1=1 ";
			
			if (qb.getSeqid() != null) {
				sqlString += " and b.Seqid = " + qb.getSeqid() + " ";
			}
			
			
			Session session = getSession();
			CategoryBig bigObj = (CategoryBig) session.createQuery(sqlString).list().get(0);
			session.close();
			
			if (bigObj != null) {
				list = new ArrayList<CategoryMedium>(bigObj.getCategoryMediumSet());
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			logger.debug(e);
		}
		
		return list;
	}
}